package com.zhulei.biji.juc;

/**
 * @description:
 * @author: zhulei
 * @create: 2025-04-25 15:54
 **/
public class OddEvenPrinter {
    private Object monitor = new Object();

    OddEvenPrinter() {
    }

    public static void main(String[] args) {

        OddEvenPrinter evenPrinter = new OddEvenPrinter();
        new Thread(()->evenPrinter.print("A"), "odd").start();
        new Thread(()->evenPrinter.print("b"), "JI").start();
    }

    private void print(String key) {
        synchronized (monitor) {
            while (true) {
                try {
                    System.out.println(key);
                    monitor.notifyAll();
                    monitor.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
